package com.hhit.yb.compose_demo.views

import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.runtime.Composable
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.hhit.yb.compose_demo.models.SectionModel
import com.hhit.yb.compose_demo.viewModel.MainViewModel
import com.hhit.yb.compose_demo.models.createComponentTutorialList
import com.hhit.yb.compose_demo.models.createGestureTutorialList
import com.hhit.yb.compose_demo.models.createGraphicsTutorialList
import com.hhit.yb.compose_demo.models.createLayoutTutorialList
import com.hhit.yb.compose_demo.models.createStateTutorialList

@OptIn(ExperimentalAnimationApi::class, ExperimentalFoundationApi::class,
    ExperimentalComposeUiApi::class
)
@Composable
fun NavGraph(
    modifier: Modifier = Modifier,
    navController: NavHostController = rememberNavController(),
    startDestination: String = Destinations.BASICS_START,
) {
    val mainViewModel = viewModel<MainViewModel>()
    if (mainViewModel.sectionList.isEmpty()) {
        val componentTutorialList: List<SectionModel> = createComponentTutorialList {
            navController.navigateUp()
        }
        val layoutTutorialList = createLayoutTutorialList()
        val stateTutorialList = createStateTutorialList()
        val gestureTutorialList = createGestureTutorialList()
        val graphicsTutorialList = createGraphicsTutorialList()

        mainViewModel.sectionList.add(componentTutorialList)
        mainViewModel.sectionList.add(layoutTutorialList)
        mainViewModel.sectionList.add(stateTutorialList)
        mainViewModel.sectionList.add(gestureTutorialList)
        mainViewModel.sectionList.add(graphicsTutorialList)
    }

    NavHost(
        modifier = modifier.statusBarsPadding(),
        navController = navController,
        startDestination = startDestination
    ) {
        composable(route = Destinations.BASICS_START) { navBackEntryStack ->
            HomeScreen(
                viewModel = mainViewModel,
                navigateToTutorial = { tutorialTitle ->
                    navController.navigate(tutorialTitle)
                }
            )
        }

        mainViewModel.sectionList.forEach { list ->
            list.forEach { model ->
                composable(route = model.title) {
                    Column(Modifier.navigationBarsPadding()) {
                        model.action?.invoke()
                    }
                }
            }
        }
    }
}

object Destinations {
    const val BASICS_START = "start_destinations"
}